import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    proxy: {
      // AI服务代理
      '/ai/api': {
        target: 'http://localhost:8081',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/ai\/api/, '/api'),
      },
      // 合同服务代理
      '/api/contract': {
        target: 'http://localhost:8007',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/contract/, '/contract'),
      },
      // 分析模块代理
      '/api/analysis': {
        target: 'http://localhost:8006',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/analysis/, '/analysis'),
      },
      // 设备服务代理
      '/api/device': {
        target: 'http://localhost:8008',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/device/, '/device'),
      },
      // 人力服务代理（开发环境直连微服务，避免网关或注册中心未就绪导致 503）
      '/api/manpower': {
        target: 'http://localhost:8004',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/manpower/, '/manpower'),
      },
      // 财务服务代理
      '/api/finance': {
        target: 'http://localhost:8001',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/finance/, '/finance'),
      },
      // 生产服务代理
      '/api/produce': {
        target: 'http://localhost:8002',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/produce/, '/produce'),
      },
      // 供应链相关统一走 supply（采购/销售/仓库/零售/盘点/商品）
      '/api/purchase': {
        target: 'http://localhost:8003',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/purchase/, '/purchase'),
      },
      '/api/sales': {
        target: 'http://localhost:8003',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/sales/, '/sales'),
      },
      '/api/warehouse': {
        target: 'http://localhost:8003',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/warehouse/, '/warehouse'),
      },
      '/api/retail': {
        target: 'http://localhost:8003',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/retail/, '/retail'),
      },
      '/api/check': {
        target: 'http://localhost:8003',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/check/, '/check'),
      },
      '/api/product': {
        target: 'http://localhost:8003',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/product/, '/product'),
      },
      // 客户/CRM 统一走 customer
      '/api/crm': {
        target: 'http://localhost:8005',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/crm/, '/crm'),
      },
      '/api/customer': {
        target: 'http://localhost:8005',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/customer/, '/customer'),
      },
      // 默认代理到网关
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    }}
})
